/*******************************************************************************
 * Copyright (c) 2011 Irineu Martins de Lima Moura (imlm2).
 * This file is part of JBeat.
 * 
 *     JBeat is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     JBeat is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with JBeat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Irineu Martins de Lima Moura (imlm2) - initial API and implementation
 ******************************************************************************/
package br.ufpe.cin.imlm2.jbeat.soot.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import soot.SceneTransformer;
import soot.SootClass;

/**
 * An abstract tagger for the Soot Scene.
 * The general idea is that a Tagger works over a set of classes (the <i>targetSet</i>) tagging either
 * the classes themselves, methods or any possible host accessible through the <i>targetSet</i>.
 * The <i>targetSet</i> resolution will depend on the constructor parameters and how the Soot Scene is
 * set up.
 * @author Irineu
 *
 */
public abstract class Tagger extends SceneTransformer {
	
	protected Collection<SootClass> targetSet;
	protected List<String> excludeClasses;
	protected List<String> includeClasses;
	protected List<String> excludePackages;
	protected boolean rtLibraries;

	protected Tagger(List<String> excludeClasses, List<String> excludePackages,
			boolean rtLibraries) {
		this(excludeClasses,new ArrayList<String>(),excludePackages,rtLibraries);
	}

	protected Tagger(List<String> excludeClasses, List<String> includeClasses,
			List<String> excludePackages, boolean rtLibraries) {
		super();
		if(excludeClasses != null)	this.excludeClasses = excludeClasses;
		else this.excludeClasses = new ArrayList<String>();
		if(includeClasses != null)	this.includeClasses = includeClasses;
		else this.includeClasses = new ArrayList<String>();
		if(excludePackages != null) this.excludePackages = excludePackages;
		else this.excludePackages = new ArrayList<String>();
		this.rtLibraries = rtLibraries;
	}

	public void tag(){
		this.internalTransform(null, null);
	}

	@Override
	protected void internalTransform(String phaseName, Map options) {
		this.targetSet = SootClassResolver.resolveClasses(this.includeClasses, excludeClasses, 
				excludePackages, rtLibraries);
		this.generateTags(this.targetSet);
		this.executePreTaggingTransformations(this.targetSet);
		this.tag(this.targetSet);
		this.executePostTaggingTransformations(this.targetSet);
	}
	
	protected abstract void tag(Collection<SootClass> classes);
	
	protected abstract void executePostTaggingTransformations(Collection<SootClass> classes);

	protected abstract void executePreTaggingTransformations(Collection<SootClass> classes);

	protected abstract void generateTags(Collection<SootClass> classes);
	
}
